import { conversionLink } from '@src/util/linkSwitch';
import { safeNewDate } from '@src/util/time';
import { t } from '@src/locales'

export default class Tab{
  constructor(options = {}){
    let closeable = options.close;
    // close closeable属性作用一样，但是closeable会覆盖close
    if(undefined !== options.closeable) closeable = options.closeable;
    // 首页不允许关闭
    if(options.url == '/home' || options.url == '/foundation/workBench') closeable = false;
    if(options.id == 'M_HOME') closeable = false;
    
    this.id = options.id || Math.random() * 10000000 >> 0;
    this.fromId = options.fromId;
    
    this.title = t('common.base.loading2');
    this.originTitle = options.title;
    this.defaultTitle = options.title; // 原始tab标题，🙅🏻‍♀️重新赋值
    
    if (options.url && !(options.isNeedConvertUrl === false)) {
      options.url = conversionLink(options.url)
    }
    
    this.url = options.url || '';
    this.currentUrl = this.url;

    this.closeable = closeable !== false;
    this.show = options.show !== false;
    this.showTime = safeNewDate().getTime();
    this.loading = true;

    this.reload = options.reload === true; // 标记tab是否需要更新
    this.noRedirect = options.noRedirect === true; // 标记tab是否不需要重定向

    this.viewType = options.viewType
    this.pageHistory = options.pageHistory
    this.pageObj = options.pageObj
  }

  merge(other = {}){
    this.reload = other.reload === true || other.url != this.currentUrl;
    this.noRedirect = other.noRedirect === true;
    this.url = other.url;
  }

  get isUrlChange(){
    return this.url != this.currentUrl;
  }

  get isHome(){
    return this.id == 'M_HOME' || this.url == '/home' || this.url == '/foundation/workBench'
  }
}